package com.webull.information.center.carwler.task.handler;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.webull.information.center.carwler.task.support.TikcerNewsProcessor;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.webull.framework.mvc.ApiException;
import com.webull.information.center.carwler.common.model.NewsInformation;
import com.webull.information.center.carwler.common.model.StockConfig;
import com.webull.information.center.carwler.common.model.StockTitle;
import com.webull.information.center.carwler.common.util.CarwlerNetWorksUtil;
import com.webull.information.center.carwler.common.util.HttpClientUtil;
import com.webull.information.center.carwler.common.util.jsoup.HttpJsoupClient;
import com.webull.information.center.carwler.task.support.StockConfigSupport;
import com.webull.information.center.common.constants.Constants;
import com.webull.information.center.common.constants.UrlConstants;
import com.webull.information.client.model.News;
import com.webull.information.client.model.parm.NewsRelType;

/**
 * google新闻处理
 *
 * @author shimingjun
 * @version 1.0
 * @since JDK 1.8
 */
@Component
public class StockGoogleNewsHandler {

    protected final Logger logger = LogManager.getLogger(getClass());

    private @Autowired
    CarwlerNetWorksUtil networksIPUtil;

    private @Autowired
    HttpClientUtil httpClientUtil;

    private @Autowired
    StockConfigSupport stockConfigSupport;

    private @Autowired
    TikcerNewsProcessor tikcerNewsProcessor;

    public void handlerByStockTitle4Google(StockTitle stockTitle) {

        String googSymbol = tikcerNewsProcessor.googleSymbol(stockTitle.getTickerId(), stockTitle.getSymbol());
        String googExchange = tikcerNewsProcessor.googleExchage(stockTitle.getExchangeId(),
                stockTitle.getExchangeAlias());

        for (int i = 0; i < Constants.googleLangArray.size(); i++) {
            String[] lang = Constants.googleLangArray.get(i);
            /**
             * 中港美股票的中文新闻 不从google抓取
             */
            // if
            // (News_SinaFinanceCnStok_Job.regionIds.contains(stockTitle.getRegionId())
            // && "zh".equalsIgnoreCase(lang[1])) {
            // continue lang;
            // } // 中港美英文使用路透源
            // else if
            // (News_ReutersEnStok_Job.regionIds.contains(stockTitle.getRegionId())
            // && "en".equalsIgnoreCase(lang[1])) {
            // continue lang;
            // }

            StockConfig stockConfig = stockConfigSupport.getStockConfig(stockTitle, "google", lang[1]);
            String compFinanceRss = getGoolgeRssUrl(googExchange, googSymbol, lang[0]);
            processGoogleRss(stockConfig, stockTitle, compFinanceRss, lang[1]);
        }
    }

    @SuppressWarnings("rawtypes")
    private void processGoogleRss(StockConfig stockConfig, StockTitle stockTitle, String compFinanceRss,
                                  String wblang) {
        try {
            List<Date> times = new ArrayList<>();
            Date lastPush = stockConfig.getCollectTime();
            SyndFeed syndFeed = httpClientUtil.retriveRssSyndFeed(compFinanceRss, true);
            if (syndFeed == null) {
                logger.warn("StockNewsHandler2 : " + stockTitle + " can't obtainSyndFeed2,compFinanceRss null:{}",
                        compFinanceRss);
                return;
            }
            List entries = syndFeed.getEntries();
            entrie:
            for (int i = 0; entries != null && i < entries.size(); i++) {
                try {
                    SyndEntry entry = (SyndEntry) entries.get(i);
                    Date push = entry.getPublishedDate();

                    if (lastPush == null || (push != null && push.after(lastPush))) {
                        times.add(push);
                        processGoogleNews(stockTitle, entry, wblang);
                    }
                } catch (Exception e) {
                    logger.warn(e);
                    break entrie;
                }
            }

            if (!times.isEmpty()) {
                tikcerNewsProcessor.updateStockConfig(stockConfig, times);
                stockConfigSupport.updateById(stockConfig);
            }
        } catch (Exception e) {
            logger.error("processRss(Date lastPush, StockTitle stockTitle, String compFinanceRss)", e);
        }
    }

    private void processGoogleNews(StockTitle stockTitle, SyndEntry entry, String wblang) throws Exception {
        String title = entry.getTitle();
        String link = entry.getLink();
        SyndContent description = entry.getDescription();
        NewsInformation info = new NewsInformation();// produce(doc);

        info.setCollectSource(News.CollectSource.Google);
        info.setSiteType(News.SiteType.PIRORIT);

        info.setTitle(title);
        info.setRegionId(stockTitle.getRegionId());
        String summary = description.getValue();
        Document summaryDoc = Jsoup.parse(summary);

        info.setSourceName(summaryDoc.getElementsByTag("span").get(0).html());
        info.setPushTime(summaryDoc.getElementsByTag("span").get(1).html());
        info.setNewsTime(entry.getPublishedDate());
        /*
		 * 概要先忽略
		 */
        info.setSummary(StringUtils.substring(summaryDoc.select("div[style~=width:80%]").first().html(), 0, 250));
        info.setUrl(link);
        String md5 = HttpJsoupClient.finishingMd5Url(link);
        info.setUrlMd5(md5);
        info.setLanguage(wblang);
        if (StringUtils.isBlank(info.getSourceName())) {
            return;
        } else if (info.getSourceName().matches("(?i)^.*Wall\\s*Street\\s*Journal.*")) {
            return;// "Wall Street Journal"
        } else if (StringUtils.equalsIgnoreCase("MarketWatch", info.getSourceName())//
                && (StringUtils.contains(link, "/investing/stock") || (StringUtils.contains(link, "/m/quote/")))) {
            return;
        }

        tikcerNewsProcessor.bindTargetNewsRecord(info, stockTitle.getTickerId(), NewsRelType.ticker);
    }

    public static void main(String[] args) {
        System.out.println("Wall Street Journal".matches("(?i)^.*Wall\\s*Street\\s*Journal.*"));
        // System.out.println(StringUtils.contains("http://www.marketwatch.com/m/quote/HLNE",
        // "/m/quote/"));
        // System.out.println(StringUtils.contains("http://www.marketwatch.com/investing/stock/glbs",
        // "/investing/stock"));
    }

    /**
     * 转换成google财经网址
     *
     * @since 0.1.0
     */
    private String getGoolgeRssUrl(String exchange, String symbol, String lang) {
        if (StringUtils.isEmpty(exchange) || StringUtils.isEmpty(symbol)) {
            throw new ApiException("getGoolgeRssUrl String exchange, String symbol is null");
        }
        if (StringUtils.equalsIgnoreCase(exchange, "HKG") && symbol.length() >= 5) {// 香港交易所,去除第一个0
            symbol = StringUtils.substring(symbol, 1);
        }
        // {googleDomain}
        String domain = networksIPUtil.getRefreshIp("googleDomain");
        return StringUtils.replaceOnce(UrlConstants.googleCompanyNewsUrl, "{googleDomain}", domain) + "q=" + exchange
                + ":" + symbol + "&output=rss&hl=" + lang;
    }
}
